package com.hog.service.article.impl;

import com.hog.constants.HogConstants;
import com.hog.dto.PageInfo;
import com.hog.dto.SharingDTO;
import com.hog.entity.BlogArticleWeb;
import com.hog.mapper.BlogArticleWebMapper;
import com.hog.service.article.IBlogArticleWebService;
import com.hog.service.common.impl.BaseServiceImpl;
import com.hog.utils.DelTagsUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Web文章Service
 *
 * @author 唐晴
 * @date 2020年08月11日
 * @since JDK 1.8
 */
@Service
public class BlogArticleWebServiceImpl extends BaseServiceImpl<BlogArticleWeb, BlogArticleWebMapper> implements IBlogArticleWebService {

    @Override
    public BlogArticleWeb getBlogArticleWeb(String id) {
        return super.mapper.getBlogArticleWeb(id);
    }

    @Override
    public List<BlogArticleWeb> newestArticleList() {
        return super.mapper.newestArticleList();
    }

    @Override
    public List<BlogArticleWeb> randomRecommendation() {
        return super.mapper.randomRecommendation();
    }

    @Override
    public List<BlogArticleWeb> list(Integer pageIndex) {
        pageIndex = (pageIndex - 1) * HogConstants.PAGE_LIST_SIZE;
        List<BlogArticleWeb> list = super.mapper.list(pageIndex, HogConstants.PAGE_LIST_SIZE);
        for (BlogArticleWeb articleWeb : list) {
            articleWeb.setDescription(DelTagsUtil.delHtmlTags(articleWeb.getDescription()));
        }
        return list;
    }

    @Override
    public PageInfo getPageInfo(Integer index) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setSize(super.mapper.count());
        pageInfo.setPages(getPages(pageInfo.getSize()));
        pageInfo.setNumberList(generateNumberList(index, pageInfo.getPages()));
        pageInfo.setCurrentIndex(index);
        // 获取上下页
        getNextAndPrePage(pageInfo);
        return pageInfo;
    }

    @Override
    public List<SharingDTO> getSharingList() {
        return super.mapper.getSharingList();
    }

    @Override
    public List<BlogArticleWeb> timeList(String date, Integer pageIndex) {
        pageIndex = (pageIndex - 1) * HogConstants.PAGE_LIST_SIZE;
        List<BlogArticleWeb> list = super.mapper.timeList(date, pageIndex, HogConstants.PAGE_LIST_SIZE);
        for (BlogArticleWeb articleWeb : list) {
            articleWeb.setDescription(DelTagsUtil.delHtmlTags(articleWeb.getDescription()));
        }

        return list;
    }

    @Override
    public PageInfo getPageInfoByTime(String date, Integer index) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setSize(super.mapper.timeCount(date));
        pageInfo.setPages(getPages(pageInfo.getSize()));
        pageInfo.setNumberList(generateNumberList(index, pageInfo.getPages()));
        pageInfo.setCurrentIndex(index);
        // 获取上下页
        getNextAndPrePage(pageInfo);
        return pageInfo;
    }

    private Integer getPages(Integer pageSize) {
        if (pageSize % HogConstants.PAGE_LIST_SIZE == 0) {
            // 整除,正好每页显示PAGE_LIST_SIZE条数
            return pageSize / HogConstants.PAGE_LIST_SIZE;
        } else {
            return pageSize / HogConstants.PAGE_LIST_SIZE + 1;
        }
    }

    /**
     * 获取上下页
     *
     * @param pageInfo
     */
    void getNextAndPrePage(PageInfo pageInfo) {
        Integer currentIndex = pageInfo.getCurrentIndex();
        Integer pages = pageInfo.getPages();

        if (pages == 1) {
            pageInfo.setPrePage(1);
            pageInfo.setNextPage(1);
            return;
        }

        if (currentIndex <= 1) {
            pageInfo.setPrePage(1);
            pageInfo.setNextPage(2);
            return;
        }

        if (currentIndex > 1 && currentIndex < pages) {
            pageInfo.setPrePage(currentIndex - 1);
            pageInfo.setNextPage(currentIndex + 1);
            return;
        }

        if (currentIndex >= pages) {
            pageInfo.setPrePage(pages - 1);
            pageInfo.setNextPage(pages);
            return;
        }
    }

    /**
     * 生成页码
     *
     * @param index
     * @param pages
     * @return
     */
    private List<Integer> generateNumberList(Integer index, Integer pages) {
        List<Integer> list = new ArrayList<>();
        if (index <= 6 && pages > 6) {
            for (int i = 1; i <= 6; i++) {
                list.add(i);
            }

            return list;
        }

        if (index <= 6 && pages <= 6) {
            for (int i = 1; i <= pages; i++) {
                list.add(i);
            }

            return list;
        }

        if (index >= 5) {
            list.add(1);
        }

        if (index < pages && index < (pages - 6)) {
            list.add(pages);
        }

        if (index > 3 && (index + 6) < pages) {
            for (int i = (index - 3); i <= (index + 3); i++) {
                list.add(i);
            }
        }

        if (index >= (pages - 6) && !index.equals(pages)) {
            for (int i = (pages - 6); i <= pages; i++) {
                list.add(i);
            }
        }

        if (index >= pages) {
            for (int i = (pages - 6); i <= pages; i++) {
                list.add(i);
            }
        }
        Collections.sort(list);
        return list;
    }
}